package org.eccl.platform.rde.web;

import javax.servlet.DispatcherType;
import javax.servlet.ServletException;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {
		try {
			DeploymentInfo servletBuilder = Servlets.deployment().setClassLoader(App.class.getClassLoader())
					.setContextPath("/").setDeploymentName("test.war")
					.addServlets(Servlets.servlet("springmvc", DispatcherServlet.class)
							.addInitParam("contextConfigLocation", "classpath:springmvc-servlet.xml").addMapping("/*"))
					.addFilters(Servlets.filter("characterEncodingFilter", CharacterEncodingFilter.class)
							.addInitParam("encoding", "UTF-8")
							.addInitParam("forceEncoding", "true"))
					.addFilterServletNameMapping("characterEncodingFilter", "springmvc", DispatcherType.REQUEST);

			DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
			manager.deploy();

			HttpHandler servletHandler = manager.start();
			PathHandler path = Handlers.path(Handlers.redirect("/")).addPrefixPath("/", servletHandler);
			Undertow server = Undertow.builder().addHttpListener(9080, "localhost").setHandler(path).build();
			server.start();
		} catch (ServletException e) {
			throw new RuntimeException(e);
		}
	}
}
